<?php

namespace yiike\company\backend\controllers;
use yiike\company\model\TtycStoreSearch;
use dmstr\bootstrap\Tabs;
use yii\web\HttpException;
use yii\helpers\Url;
use yii\filters\AccessControl;
use common\models\Region;

/**
* This is the class for controller "TtycStoreController".
*/
class TtycStoreController extends \yiike\company\backend\controllers\base\TtycStoreController
{
    public function actionIndex()
    {
        $_GET['TtycStoreSearch']['company_id'] = \Yii::$app->user->identity->id;
        $searchModel  = new TtycStoreSearch;
        $dataProvider = $searchModel->search($_GET);

        Tabs::clearLocalStorage();

        Url::remember();
        \Yii::$app->session['__crudReturnUrl'] = null;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    public function actionUpdate($id)
    {
        $children = array();


        $category = \Yii::$app->db->createCommand("SELECT id,name,parentid FROM ims_ttyc_category WHERE weid = 2 and parentid=0 ORDER BY id DESC")->queryAll();
        $cate = \Yii::$app->db->createCommand("SELECT id,name,parentid FROM ims_ttyc_category WHERE weid = 2 and parentid!=0 ORDER BY id DESC")->queryAll();

        foreach($category as $k=>$v){
            $arr[$v['id']] = $v['name'];
        }
        foreach($arr as $k=>$v){
            foreach($cate as $m=>$n){
                if($n['parentid']==$k){
                    $arr[$v][$n['id']]=$n['name'];
                }
            }
        }
        $category = $arr;

        $model = $this->findModel($id);
        $model->province = Region::getCityId($model->location_p);
        $model->city = Region::getCity($model->location_c,$model->province);
        $model->district = Region::getCity($model->location_a,$model->city);
        if ($model->load($_POST)) {

            $province = $_POST['TtycStore']['province'];
            $province = Region::getCityName($province);
            $city = $_POST['TtycStore']['city'];
            $city = Region::getCityName($city);
            $district = $_POST['TtycStore']['district'];
            $district = Region::getCityName($district);
            $cate = $_POST['TtycStore']['ccate'];
            $parent = \Yii::$app->db->createCommand("SELECT parentid FROM ims_ttyc_category WHERE weid = 2 and id=$cate ")->queryOne();
            $parent = $parent['parentid'];
            if($parent==0){
                $model->pcate = $cate;
                $model->ccate = $cate;
            }else{
                $model->pcate = $parent;
                $model->ccate = $cate;
            }
            $model->location_p = $province;
            $model->location_c = $city;
            $model->location_a = $district;
            if($model->save()){
                return $this->redirect(['ttyc-store/index']);
            }else{
                return $this->render('update', [
                    'model' => $model,
                    'category'=>$category,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
                'category'=>$category,
            ]);
        }
    }
    //省市县三级
    public function actions()
    {
        $actions=parent::actions();
        $actions['get-region']=[
            'class'=>\chenkby\region\RegionAction::className(),
            'model'=>\common\models\Region::className()
        ];
        return $actions;
    }
}
